// 显示编辑用户模态框
function showEditUserModal(userId) {
    fetch(`/admin/users/${userId}`)
        .then(response => response.json())
        .then(data => {
            const form = document.getElementById('editUserForm');
            form.id.value = data.id;
            form.username.value = data.username;
            form.nickname.value = data.nickname || '';
            form.role.value = data.role;
            
            document.getElementById('editUserModal').style.display = 'block';
        });
}

// 关闭模态框
function closeModal(modalId) {
    document.getElementById(modalId).style.display = 'none';
}

// 编辑用户
document.addEventListener('DOMContentLoaded', function() {
    const editForm = document.getElementById('editUserForm');
    if (editForm) {
        editForm.addEventListener('submit', function(e) {
            e.preventDefault();  // 阻止表单默认提交行为
            console.log('Form submitted');  // 调试日志
            
            const formData = new FormData(this);
            const userId = formData.get('id');
            const nickname = formData.get('nickname');
            const role = formData.get('role');
            
            console.log('Updating user:', { userId, nickname, role });  // 调试日志
            
            // 构建请求参数
            const params = new URLSearchParams();
            params.append('nickname', nickname);
            params.append('role', role);
            
            fetch(`/admin/users/${userId}/update`, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
                },
                body: params.toString()
            })
            .then(response => {
                console.log('Response status:', response.status);  // 调试日志
                if (!response.ok) {
                    throw new Error(`HTTP error! status: ${response.status}`);
                }
                return response.json();
            })
            .then(data => {
                console.log('Response data:', data);  // 调试日志
                if (data.success) {
                    alert('更新成功');
                    window.location.reload();  // 直接刷新当前页面
                } else {
                    alert(data.message || '更新失败');
                }
            })
            .catch(error => {
                console.error('Error:', error);
                alert('更新失败，请重试');
            })
            .finally(() => {
                closeModal('editUserModal');  // 无论成功失败都关闭模态框
            });
        });
    } else {
        console.error('Edit form not found');  // 调试日志
    }
});

// 删除用户
function deleteUser(userId) {
    if (!confirm('确定要删除该用户吗？此操作不可恢复！')) {
        return;
    }
    
    fetch(`/admin/users/${userId}/delete`, {
        method: 'POST'
    })
    .then(response => response.json())
    .then(data => {
        if (data.success) {
            alert('删除成功');
            location.reload();
        } else {
            alert(data.message || '删除失败');
        }
    });
} 